#include "animation.h"
#include <stdlib.h>

animation_t* create_animation(sprite_t *spr){
    animation_t* ret = NULL;

    if(spr == NULL){return NULL;}
    ret = (animation_t*)malloc(sizeof(animation_t*));
    ret->spr = spr;
    ret->locs = NULL;
    ret->loc_num = 0;

    return ret;
}

void free_animation_chain(animation_t *ac){
    if(ac == NULL){return;}
    if(ac->spr != NULL){
        free(ac->spr);
        ac->spr = NULL;
    }
    if(ac->locs != NULL){
        free(ac->locs);
        ac->locs = NULL;
    }
    ac->loc_num = 0;
    free(ac);
}

void append_animation(animation_t *an, const texture_location_t loc){
    texture_location_t *tl;
    if(an == NULL){return;}

    tl = realloc(an->locs, (an->loc_num+1) * sizeof(texture_location_t));

    if(tl == NULL){
        return;
    }

    an->locs = tl;
    an->locs[an->loc_num] = loc;
    an->loc_num++;
}

void write_animation_text(FILE *out, animation_t *an){
    int i;

    if(out == NULL || an == NULL){return;}
    fprintf(out, "%h", an->loc_num);
    for(i = 0; i < an->loc_num; i++){
        fprintf(out, " %h %h %h %h", an->locs[i].x, an->locs[i].y,
                an->locs[i].w, an->locs[i].h);
    }
    fprintf(out, "\n");
}

void write_animation_bin(FILE *out, animation_t *an){
    fprintf(stderr, "write_animation_bin not written\n");
}
